package com.example.tuike.controller;

import com.example.tuike.entity.UserArticles;
import com.example.tuike.service.UserArticlesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/*** @Author supersaiya
 * @Description TODO

 * @Date 2025/2/2 21:27

 * @Version 1.0*/
@RestController
@RequestMapping("/api/userArticles")
public class UserArticlesController {

    @Autowired
    private UserArticlesService userArticlesService;

    @GetMapping
    public ResponseEntity<List<UserArticles>> getAllUserArticles() {
        List<UserArticles> userArticles = userArticlesService.getAllUserArticles();
        return ResponseEntity.ok(userArticles);
    }

    @GetMapping("/{id}")
    public ResponseEntity<UserArticles> getUserArticleById(@PathVariable Long id) {
        UserArticles userArticle = userArticlesService.getUserArticleById(id);
        if (userArticle == null) {
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(userArticle);
    }

    @PostMapping
    public ResponseEntity<UserArticles> createUserArticle(@RequestBody UserArticles userArticle) {
        UserArticles createdUserArticle = userArticlesService.createUserArticle(userArticle);
        return ResponseEntity.ok(createdUserArticle);
    }

    @PutMapping("/{id}")
    public ResponseEntity<UserArticles> updateUserArticle(@PathVariable Long id, @RequestBody UserArticles userArticle) {
        userArticle.setUserArticleId(id);
        UserArticles updatedUserArticle = userArticlesService.updateUserArticle(userArticle);
        return ResponseEntity.ok(updatedUserArticle);
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteUserArticle(@PathVariable Long id) {
        userArticlesService.deleteUserArticle(id);
        return ResponseEntity.noContent().build();
    }
}
